﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaPortal.GUI.Library;

namespace Burn4You
{
    public class FilesSelectionScreen : GUIWindow
    {

        #region GUI Controls

        [SkinControlAttribute(1)]        protected GUIButtonControl btnBack = null;
        [SkinControlAttribute(2)]        protected GUIButtonControl btnNext = null;
        
        [SkinControlAttribute(10)]        protected GUIButtonControl btnSelectFile = null;
        [SkinControlAttribute(50)]        protected GUIListControl lstBurnFiles = null;
        [SkinControlAttribute(51)]        protected GUIListControl lstFilesOnDisk = null;

        #endregion

        #region Variables

        private List<BurnFile> KnownFiles;
        /// <summary>
        /// ID of the last window to call this screen.
        /// </summary>
        public static int CallingWindow;
        /// <summary>
        /// This indicates if the users completed the window (by clicking on the next button). If false, ignore the files selected from this window.
        /// </summary>
        public static bool Completed = false;
        /// <summary>
        /// This is a list of files types you want the window to present to the user. Seperate the types with a "," without quotes. E.G. "Movie,TV Show,Video".
        /// </summary>
        public static String FileTypesToGet = "";
        /// <summary>
        /// The list of files the user chose.
        /// </summary>
        public static List<BurnFile> ChosenFiles;
        /// <summary>
        /// Type of disk the files are for. Allows the screen to show a "disk space left" bar.
        /// </summary>
        public static DiskType diskType;

        #endregion

        #region GUIWindow overloads
        
        protected override void OnPageLoad()
        {
            Utils.Burn4YouLog.Write("File Selection Screen: Loading Page");

            Completed = false;

            CallingWindow = GUIWindowManager.GetPreviousActiveWindow();

            KnownFiles = Burn4YouPlugin.GetBurnFilesByType(FileTypesToGet);
            ChosenFiles = new List<BurnFile>();

            FillKnownFilesGUIList();
            FillFilesOnDiskGUIList();

            Utils.setGUIProperty("#Burn4YouVideoDisk.DiskType", diskType.ToString());

            base.OnPageLoad();
        }

        public override bool Init()
        {
            ChosenFiles = new List<BurnFile>();

            return Load(GUIGraphicsContext.Skin + @"\Burn4YouFileSelectionScreen.xml");
        }

        public override int GetID
        {
            get
            {
                return 54264;
            }
        }

        protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == btnSelectFile)
            {
                String File = Utils.SelectFile();
                Utils.Burn4YouLog.Write("Video Disk Extension: Custom file selected is: " + File);

                BurnFile bf = new BurnFile();

                // get file info here, when done, fill BurnFile object and the GUIListItem with the info

                bf.FilePath = File;
                bf.Name = File.Remove(0, File.LastIndexOf('\\'));

                ChosenFiles.Add(bf);

                GUIListItem item = new GUIListItem();
                item.TVTag = bf;
                item.Label = bf.FilePath;

                lstFilesOnDisk.Add(item);
            }
            else if (control == lstBurnFiles)
            {
                GUIListItem item = lstBurnFiles.SelectedListItem;

                lstBurnFiles.RemoveItem(lstBurnFiles.SelectedListItemIndex);
                KnownFiles.Remove((BurnFile)item.TVTag);

                ChosenFiles.Add((BurnFile)item.TVTag);
                lstFilesOnDisk.Add(item);

                Utils.Burn4YouLog.Write("File Selection Screen: User added file \"" + item.Label + "\"");
            }
            else if (control == lstFilesOnDisk)
            {
                GUIListItem item = lstFilesOnDisk.SelectedListItem;

                lstFilesOnDisk.RemoveItem(lstFilesOnDisk.SelectedListItemIndex);
                ChosenFiles.Remove((BurnFile)item.TVTag);

                KnownFiles.Add((BurnFile)item.TVTag);
                lstBurnFiles.Add(item);
                Utils.Burn4YouLog.Write("File Selection Screen: User removed file \"" + item.Label + "\"");
            }
            else if (control == btnBack)
            {
                Utils.Burn4YouLog.Write("File Selection Screen: User closed screen.");
                GUIWindowManager.ShowPreviousWindow();
            }
            else if (control == btnNext)
            {
                Utils.Burn4YouLog.Write("File Selection Screen: User completed screen.");
                Completed = true;
                GUIWindowManager.ShowPreviousWindow();
            }

            base.OnClicked(controlId, control, actionType);
        }

        protected override void OnPageDestroy(int new_windowId)
        {
            Utils.Burn4YouLog.Write("File Selection Screen: Screen being destroyed.");
            base.OnPageDestroy(new_windowId);
        }

        #endregion

        #region Functions

        private void FillKnownFilesGUIList()
        {
            lstBurnFiles.Clear();

            foreach (BurnFile file in KnownFiles)
            {
                GUIListItem item = new GUIListItem();
                item.Label = file.Name;
                item.Label2 = file.BurnType;
                item.TVTag = file;

                lstBurnFiles.Add(item);
            }
        }

        private void FillFilesOnDiskGUIList()
        {
            lstFilesOnDisk.Clear();

            foreach (BurnFile file in ChosenFiles)
            {
                GUIListItem item = new GUIListItem();
                item.Label = file.Name;
                item.Label2 = file.BurnType;
                item.TVTag = file;

                lstFilesOnDisk.Add(item);
            }
        }

        #endregion
    }
}
